package com.etime.shuhuiyuan.controllers.department;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shuhuiyuan.common.BaseMessage;
import com.etime.shuhuiyuan.common.ListMessage;
import com.etime.shuhuiyuan.common.PageMessage;
import com.etime.shuhuiyuan.common.PojoMessage;
import com.etime.shuhuiyuan.pojo.Demo;
import com.etime.shuhuiyuan.pojo.Department;
import com.etime.shuhuiyuan.services.demo.interfaces.DemoService;
import com.etime.shuhuiyuan.services.department.interfaces.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * department示例的controller控制层
 */
@Api(tags = "department案例接口")
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;


    @ApiOperation("添加department案例")
    @ApiImplicitParams({
            @ApiImplicitParam(name="deptid",value = "自增长主键，不传递"),
            @ApiImplicitParam(name="deptname",value="传递字符串"),
            @ApiImplicitParam(name="deptstate",value = "状态，0-不可用；1-正常"),
            @ApiImplicitParam(name="deptuserid",value="传递数字"),
            @ApiImplicitParam(name="deptdesc",value = "传递字符串"),
            @ApiImplicitParam(name="deptusername",value = "传递部门负责人姓名")
    })
    @PostMapping("/insert")
    public BaseMessage insertDepartment(Department department){
        BaseMessage msg = new BaseMessage();
        int rtn = departmentService.insertDepartment(department);
        msg.setCode(rtn);
        if(rtn>0){
            msg.setMsg("保存成功");
        }else{
            msg.setMsg("保存失败");
        }
        return msg;
    }

    @ApiOperation("修改案例")
    @ApiImplicitParams({
            @ApiImplicitParam(name="deptid",value = "需要修改的id，必须传递"),
            @ApiImplicitParam(name="deptname",value="传递字符串"),
            @ApiImplicitParam(name="deptstate",value = "状态，0-不可用；1-正常"),
            @ApiImplicitParam(name="deptuserid",value="传递数字"),
            @ApiImplicitParam(name="deptdesc",value = "传递字符串"),
            @ApiImplicitParam(name="deptusername",value = "传递部门负责人姓名")
    })
    @PostMapping("/update")
    public BaseMessage updateDepartment(Department department){
        BaseMessage msg = new BaseMessage();
        int rtn = departmentService.updateDepartment(department);
        msg.setCode(rtn);
        if(rtn>0){
            msg.setMsg("保存成功");
        }else{
            msg.setMsg("保存失败");
        }
        return msg;
    }
    @ApiOperation("查询列表案例")
    @ApiImplicitParams({
            @ApiImplicitParam(name="deptname",value="传递字符串"),
            @ApiImplicitParam(name="deptstate",value = "状态，0-不可用；1-正常")
    })
    @GetMapping("/list")
    public ListMessage<Department> selectDepartmentList(Department department){
            ListMessage<Department> listMessage = new ListMessage<>();
            List<Department> list = departmentService.selectDepartmentList(department);
            if(list!=null) {
                listMessage.setDatalist(list);
                listMessage.setCode(1);
            }else{
                listMessage.setCode(0);
                listMessage.setMsg("没有查询到数据");
            }
            return listMessage;
    }

    @ApiOperation("分页查询案例")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptname",value="传递字符串"),
            @ApiImplicitParam(name = "deptstate",value = "状态，0-不可用；1-正常"),
            @ApiImplicitParam(name = "pagenum",value = "第几页"),
            @ApiImplicitParam(name = "pagesize",value = "每页多少行")}
    )
    @GetMapping("/page")
    public PageMessage<Department> selectRoleByPage(Department department, Long pagenum, Long pagesize){
        Page<Department> pagedata = departmentService.selectDepartmentByPage(department,pagenum,pagesize);
        PageMessage<Department> msg = new PageMessage<>();
        if(pagedata!=null) {
            msg.setCode(1);
            msg.setPagedata(pagedata);
        }else{
            msg.setCode(0);
            msg.setMsg("没有查询到数据");
        }
        return msg;
    }

    @ApiOperation("根据id查询一条department信息的案例")
    @ApiImplicitParam("deptid")
    @GetMapping("/get/{deptid}")
    public PojoMessage<Department> selectDepartmentById(@PathVariable("deptid") int deptid){
        PojoMessage<Department> msg = new PojoMessage<>();
        Department department = departmentService.selectDepartmentByid(deptid);
        if(department!=null){
            msg.setCode(1);
            msg.setData(department);
        }else{
            msg.setCode(0);
            msg.setMsg("没有找到指定数据");
        }
        return msg;
    }
}
